<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $user_id
 * @property string $owner_id
 * @property string $accept
 * @property string $date_join
 * @property string $date_active
 * @property string $date_review
 * @property string $reviewer
 * @property string $payment_id
 * @property string $ip_active
 * @property string $client_browser
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id,website_id,adv_id', 'required'),
			array('user_id,website_id,adv_id,ordernum,adv_click_history_id', 'length', 'max'=>12),
			array('order_amount', 'numerical'),
			array('owner_id, ip_active', 'length', 'max'=>20),
			array('website', 'length', 'max'=>50),
			array('referer', 'length', 'max'=>255),
			array('accept, payment_status', 'length', 'max'=>1),
			array('reviewer, payment_id', 'length', 'max'=>50),
			array('client_browser', 'length', 'max'=>250),
			array('date_join, date_active, date_review', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, owner_id, accept, date_join, date_active, date_review, reviewer, payment_id, ip_active, client_browser', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'Member ID',
			'owner_id' => 'Owner',
			'adv_id' => 'Adv',
			'website_id' => 'Website ID',
			'website' => 'Website',
			'adv_click_history_id' => 'Adv Click History ID',
			'referer' => 'Referer Page',
			'accept' => 'Accept',
			'ordernum' => 'Order Count',
			'order_amount' => 'Order Amount',
			'date_join' => 'Date',
			'date_active' => 'Date Active',
			'date_review' => 'Date Review',
			'reviewer' => 'Reviewer',
			'payment_id' => 'Payment',
			'ip_active' => 'Ip Active',
			'client_browser' => 'Client Browser',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('owner_id',$this->owner_id,true);
		$criteria->compare('adv_id',$this->adv_id,true);
		$criteria->compare('website_id',$this->website_id,true);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('accept',$this->accept,true);
		$criteria->compare('date_join',$this->date_join,true);
		$criteria->compare('ordernum',$this->ordernum,true);
		$criteria->compare('order_amount',$this->order_amount,true);
		$criteria->compare('date_active',$this->date_active,true);
		$criteria->compare('date_review',$this->date_review,true);
		$criteria->compare('reviewer',$this->reviewer,true);
		$criteria->compare('payment_id',$this->payment_id,true);
		$criteria->compare('ip_active',$this->ip_active,true);
		$criteria->compare('client_browser',$this->client_browser,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 获得某个加盟账号所带来的全部会员数
	 * @param int $owner_id
	 */
	public static function getLeads($owner_id) {
		return self::model()->count('owner_id=:owner_id', array(':owner_id' => $owner_id));
	}
	
	public static function getNumByAccept($accept) {
		return self::model()->count('accept=:accept', array(':accept' => $accept));
	}
}